﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Tools
{
    public static class StringExtension
    {
        /// <summary>
        /// 将路径映射成url
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string PathMapToUrl(this string path)
        {
           if(ToolSimpleCheck.IsNullOrEmpty(path))
            {
                return path;
            }
            ///F:\MyProject\C#\WebApp\Platform\MDT.VirtualSoftPlatform\MDT.VirtualSoftPlatform.ServiceInstance\bin\Debug\net5.0\wwwroot\resource\unity
            string[] array = path.Split(SimpleConstCode.UnSprit);
            int start = IndexOfStrArray(array, SimpleConstCode.Wwwroot);
            start++;
            int length = array.Length - start;
            return JoinStringArray(array, start, length, SimpleConstCode.Sprit);
        }
        /// <summary>
        /// 将url转换路径
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string UrlMapToPath(this string url)
        {
            if (ToolSimpleCheck.IsNullOrEmpty(url))
            {
                return url;
            }
            url = url.Replace(SimpleConstCode.Sprit, SimpleConstCode.UnSprit);
            return System.IO.Path.Combine(SimpleConstCode.Wwwroot, url);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static int IndexOfStrArray(string[] array, string target)
        {
            if (ToolSimpleCheck.IsNullOrEmpty(array)|| string.IsNullOrEmpty(target))
            {
                return -1;
            }
            for (int i = 0; i < array.Length; i++)
            {
                if (target.Equals(array[i]))
                {
                    return i;
                }
            }
            return -1;
        }
        /// <summary>
        /// 连接字符数组
        /// </summary>
        /// <param name="array"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static string JoinStringArray(string[] array, int start, int length, char pattern = ' ')
        {
            if (ToolSimpleCheck.IsNullOrEmpty(array))
            {
                return string.Empty;
            }

            if (start >= 0 && start + length < array.Length)
            {
                throw new ArgumentOutOfRangeException("");
            }
            StringBuilder sb = new StringBuilder();
            for (int i = start; i < start + length; i++)
            {
                if (i + 1 == start + length)
                {
                    sb.Append($"{array[i]}");
                }
                else
                {
                    sb.Append($"{array[i]}{pattern}");
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 将ip  转为int long,如果包含冒号将会把冒号去掉
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static long IpToInt(this string ip)
        {
            if (ToolSimpleCheck.IsNullOrEmpty(ip))
            {
                return 0;
            }
            if (ip.Contains(SimpleConstCode.Colon))
            {
                string[] strs = ip.Split(SimpleConstCode.Colon);
                foreach (var str in strs)
                {
                    if (str.Contains(SimpleConstCode.Dot))
                    return GetIpToInt(str);
                }
            }
            if (ip.Contains(SimpleConstCode.Colon))
            {
                return GetIpToInt(ip);
            }
            return 0;
        }

        public static string GetIpV4(this string ip)
        {
            if (ToolSimpleCheck.IsNullOrEmpty(ip))
            {
                return string.Empty;
            }
            if (ip.Contains(SimpleConstCode.Colon))
            {
                string[] strs = ip.Split(SimpleConstCode.Colon);
                foreach (var item in strs)
                {
                    if (item.Contains(SimpleConstCode.Dot))
                    {
                        return item;
                    }
                }
            }
            return ip;
        }
        /// <summary>
        /// 将ip 转换成long int
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        private static long GetIpToInt(string ip)
        {

            if (ToolSimpleCheck.IsNullOrEmpty(ip))
            {
                return 0;
            }
                char[] separator = new char[] { SimpleConstCode.Dot};
                string[] items = ip.Split(separator);
                if (items.Length >= 4)
                {
                    return items[0].ConvertToLong() << 24
                            | items[1].ConvertToLong() << 16
                            | items[2].ConvertToLong() << 8
                            | items[3].ConvertToLong();
                }
            return 0;
        }
        public static long ConvertToLong(this string str)
        {
            long.TryParse(str, out long res);
            return res;
        }
        /// <summary>
        /// 截取
        /// </summary>
        /// <param name="str"></param>
        /// <param name="startStr"></param>
        /// <param name="endStr"></param>
        /// <returns></returns>
        public static string CutOut(this string str, string startStr, string endStr)
        {
            if (ToolSimpleCheck.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            if (ToolSimpleCheck.IsNullOrEmpty(startStr)|| ToolSimpleCheck.IsNullOrEmpty(endStr))
            {
                return str;
            }
            if (str.Contains(startStr) == false && str.Contains(endStr) == false)
            {
                return string.Empty;
            }
            int start = str.IndexOf(startStr) + 2;
            int length = str.IndexOf(endStr) - start;
            return str.Substring(start, length);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <param name="c">分割符号</param>
        /// <returns></returns>
        public static int[] ToIntArray(this string str, char c)
        {
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            int[] array = null;
            if (str.Contains(c))
            {
                array = str.Split(c).Select(t => { int res = 0; int.TryParse(t, out res); return res; }).ToArray();
            }
            else
            {
                int res = 0;
                int.TryParse(str, out res);
                array = new int[1];
                array[0] = res;
            }
            return array;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Guid StringToGuid(this string value)
        {
            if (ToolSimpleCheck.IsNullOrEmpty(value))
            {
                return Guid.Empty;
            }
            if (Guid.TryParse(value, out Guid guid))
            {
                return guid;
            }
            return Guid.Empty;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="values"></param>
        /// <param name="splitSign"></param>
        /// <returns></returns>
        public static int[] StringToIntArray(this string values, char splitSign)
        {
            int[] result = null;
            if (ToolSimpleCheck.IsNull(values) || ToolSimpleCheck.IsNull(splitSign))
            {
                return result;
            }
            if (values.Contains(splitSign))
            {
                result = values.Split(splitSign).Select(t => { int.TryParse(t, out int value); return value; }).ToArray();
            }
            else
            {
                int.TryParse(values, out int value);
                result = new int[] { value };
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="splitSign"></param>
        /// <returns></returns>

        public static string SprlitGetFirstChar(this string value,char splitSign)
        {
            if (ToolSimpleCheck.IsNullOrEmpty(value))
            {
                return string.Empty;
            }
            var array= value.StringToArray(splitSign);
            StringBuilder sb=new StringBuilder();

            sb.Append(string.Join('.', array.Select(item =>
              {
                  if (!ToolSimpleCheck.IsNullOrEmpty(item))
                  {
                      return item[0].ToString();
                  }
                  else
                  {
                      return string.Empty;
                  }
              }
             )));
           
            return sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="values"></param>
        /// <param name="splitSign"></param>
        /// <returns></returns>
        public static Guid[] StringToGuidArray(this string values, char splitSign)
        {
            Guid[] result = null;
            if (ToolSimpleCheck.IsNull(values) || ToolSimpleCheck.IsNull(splitSign))
            {
                return result;
            }
            if (values.Contains(splitSign))
            {
                result = values.Split(splitSign).Select(t => { Guid.TryParse(t, out Guid value); return value; }).ToArray();
            }
            else
            {
                Guid.TryParse(values, out Guid value);
                result = new Guid[] { value };
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="values"></param>
        /// <param name="splitSign"></param>
        /// <returns></returns>
        public static string[] StringToArray(this string values, char splitSign)
        {
            string[] result = null;
            if (ToolSimpleCheck.IsNull(values) || ToolSimpleCheck.IsNull(splitSign))
            {
                return result;
            }
            if (values.Contains(splitSign))
            {
                result = values.Split(splitSign).Select(t => t).ToArray();
            }
            else
            {
                result = new string[] { values };
            }
            return result;
        }


		public static long ToInt64(this string value)
		{
            if (string.IsNullOrEmpty(value))
            {
                return 0l;
            }
            if (long.TryParse(value,out long lvalue))
            {
				return lvalue;
			}
            return 0l;
		}
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
		public static char PathChar()
		{
			if (ToolSimpleCheck.PlatformIsWindows())
			{
				return '\\';
			}
			else if (ToolSimpleCheck.PlatformIsLinux())
			{
				return '/';
			}
			else
			{
				throw new Exception("PathToUrl  方法不支持该系统!");
			}
		}
        /// <summary>
        /// 根据操作系统返回路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
		public static string GetPath(this string path)
        {
            if (ToolSimpleCheck.PlatformIsWindows())
            {
               
                return path.Replace("/", "\\");
            }
            else if (ToolSimpleCheck.PlatformIsLinux())
            {
                return path.Replace("\\", "/");
            }

            else
            {
				throw new Exception("PathToUrl  方法不支持该系统!");
			}
		}

	}
}
